import QtQuick 2.9
import QtQuick.Controls 2.2

import InputMethod 1.0

ApplicationWindow {
    id: appWindow
    visible: true
    width: 1024
    height: 600
    title: qsTr("Hello World")

    Grid {
        id: rootGrid
        spacing: 10
        columns: 8
        y: 0
        Switch {
            id: virtualKeyboardSwitch
            text: "虚拟键盘"
            checked: false
        }
        Button {
            text: "重置数据库"
            onClicked: {
                Input.resetDatabase()
            }
        }
        TextField {
            placeholderText: "DigitsOnly"
            inputMethodHints: Qt.ImhDigitsOnly
        }
        TextField {
            placeholderText: "Password"
            echoMode: TextInput.Password
        }
        TextField {
            text: "ReadOnly"
            readOnly: true
        }

        Repeater {
            model: 100
            delegate: TextField {
                placeholderText: index
            }
        }
    }

    Component.onCompleted: Input.setlistenRoot(this)
    onActiveFocusControlChanged: {
        //        console.log("activeFocusControl:", activeFocusControl)
        if (inputMethodIndicator.active)
            inputMethodIndicator.item.call(activeFocusControl)
        if (virtualKeyboard.active)
            virtualKeyboard.item.call(activeFocusControl, 120)
    }

    InputMethodViewer {
        id: inputMethod
        onChineseChanged: {
            if (inputMethodIndicator.active) {
                inputMethodIndicator.item.chinese = chinese
            }
            if (virtualKeyboard.active) {
                virtualKeyboard.item.chinese = chinese
            }
        }
    }
    Loader {
        id: inputMethodIndicator
        active: !virtualKeyboardSwitch.checked
        source: "InputMethodIndicator.qml"
    }

    Loader {
        id: virtualKeyboard
        active: virtualKeyboardSwitch.checked
        source: "VirtualKeyboard.qml"
        anchors.fill: parent
        Connections {
            target: inputMethod
        }
        onLoaded: {
            item.root = rootGrid
            item.window = appWindow
        }
    }
}
